package com.tarena.lbs.attach.utils;

import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class ImageDimensionUtils {

    public static ImageDimension getImageDimension(MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            return null;
        }

        try (InputStream inputStream = file.getInputStream()) {
            BufferedImage image = ImageIO.read(inputStream);

            if (image == null) {
                throw new IllegalArgumentException("无法读取图片文件，可能不是支持的图片格式");
            }

            int width = image.getWidth();
            int height = image.getHeight();

            return new ImageDimension(width, height);
        }
    }

    // 封装图片尺寸的类
    public static class ImageDimension {
        private final int width;
        private final int height;

        public ImageDimension(int width, int height) {
            this.width = width;
            this.height = height;
        }

        // getter方法
        public int getWidth() { return width; }
        public int getHeight() { return height; }

        @Override
        public String toString() {
            return width + "x" + height;
        }
    }
}